<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: smallWhite
 * @Date: 2023-05-12 20:36:16
 * @LastEditors: smallWhite
 * @LastEditTime: 2023-06-07 17:52:01
 * @FilePath: /siana_admin/src/views/system/client/index.vue
-->
<template>
  <el-card>
    <div slot="header">
      <h2>客户端信息配置</h2>
    </div>
    <el-form ref="ruleForm"
      :model="form"
      :rules="rules"
      label-width="180px"
      :label-position="phone?'top':'right'"
      class="demo-ruleForm">

      <el-form-item
        label="客户端名称"
        prop="clientName">
        <el-input
          v-model="form.clientName"
          :disabled="disabled"
          style="width:100%;max-width: 600px;" />
      </el-form-item>
      <el-form-item
        label="客户端LOGO"
        prop="clientLogo">
        <el-upload
          class="avatar-uploader"
          :action="action"
          :disabled="disabled"
          :headers="headers"
          :on-success="handleSuccess"
          :show-file-list="false">
          <img
            v-if="form.imageUrl"
            :src="form.imageUrl"
            class="avatar" />
          <i v-else
            class="el-icon-upload avatar-uploader-icon"></i>
        </el-upload>
        <el-input
          v-show="false"
          v-model="form.clientLogo"
          :disabled="disabled"
          style="width:100%;max-width: 600px;" />
      </el-form-item>
      <el-form-item
        v-if="!disabled">
        <el-button
          type="primary"
          @click="handleSubmit()">保存</el-button>
        <el-button
          @click="handleCancel()">取消</el-button>
      </el-form-item>
      <el-form-item v-else>
        <el-button
          type="primary"
          @click="disabled = !disabled">去编辑</el-button>

      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { HTTP_URI } from '@/api_new/api'
export default {
  data() {
    return {
      phone: false,
      form: {
        imageUrl: '',
        isOpenMj: 0, // 注册模式  1 账号密码 2 邮箱注册 3 公众号
        clientLogo: '', // gpt请求地址
        clientName: '' // discord频道id
      },
      action: HTTP_URI.UPLOADIMG,
      headers: {
        token: window.localStorage.getItem('token')
      },
      disabled: true,
      rules: {
        clientLogo: [{ required: false, message: '请输入', trigger: 'blur' }],
        clientName: [{ required: false, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.phone = JSON.parse(window.localStorage.getItem('phone'))
    this.getData()
  },
  methods: {
    getData() {
      this.$https('SYSTEMSETTING', {}).then(res => {
        this.form = {
          id: res.data.id,
          clientLogo: res.data.clientLogo, // gpt请求地址
          clientName: res.data.clientName, // discord频道id
          imageUrl: res.data.imgReturnUrl + res.data.clientLogo
        }
      })
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$https('SYSTEMEDIT', this.form).then(res => {
            if (res.status == 200) {
              this.$message.success('编辑成功！')
              this.disabled = true
            }
          })
        }
      })
    },
    handleSuccess(res, file) {
      console.log(res.data)
      this.form.clientLogo = res.data.filePatch
      this.form.imageUrl = res.data.imageUrl
    },
    handleCancel() {
      this.form = {
        id: '',
        imageUrl: '',
        clientLogo: '', // gpt请求地址
        clientName: '' // discord频道id
      }
      this.getData()
      this.disabled = true
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 130px;
  height: 130px;
  line-height: 130px;
  text-align: center;
}
.avatar {
  width: 130px;
  height: 130px;
  display: block;
}
</style>
